package com.example.yuejian.yuejian.adapter;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.activity.TeacherDetailActivity;
import com.example.yuejian.yuejian.bean.Collection;
import com.example.yuejian.yuejian.bean.MyFollowMaster;
import com.example.yuejian.yuejian.model.CollectionModel;
import com.example.yuejian.yuejian.view.RoundImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

/**
 * Created by Administrator on 2017/9/18.
 */

public class MyFollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private Context mContext;;
    private List mDataList;
    public LayoutInflater mLayoutInflater;
    int userid;



    public MyFollowAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.cardview_my_follow, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        final MyFollowMaster entity = (MyFollowMaster) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.myfollw_name.setText(entity.getName());
        viewHolder.myfollw_intro.setText(entity.getIntro());
        if(!"".equals(entity.getPhoto()))
        {
            Picasso.with(mContext).load(entity.getPhoto()).into(viewHolder.imageView_photo);
        }
        else {
            viewHolder.imageView_photo.setImageResource(R.drawable.camera);
        }



        viewHolder.myfollow_master_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, TeacherDetailActivity.class);
                intent.putExtra("master_id",entity.getId()+"");
                mContext.startActivity(intent);

            }
        });

        viewHolder.btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUser_id();
               int uid=userid;
                CollectionModel Model=new CollectionModel();
                Call call=Model.CancelMyfollow(uid,3,entity.getId());
                call.enqueue(new Callback<Collection>() {
                    @Override
                    public void onResponse(Call<Collection> call, retrofit2.Response<Collection> response)
                    {
                        Collection tmp=response.body();
                        ;
                        if ( tmp.getRetCode()==1)
                        {
                            Toast.makeText(mContext, "取消收藏成功！", Toast.LENGTH_SHORT).show();
                            //if (String.valueOf(entity.getFood_id()).equals(String.valueOf(foodid)))
                            mDataList.remove(position);
                            notifyDataSetChanged();
                        }
                        else
                        {
                            Toast.makeText(mContext, "取消失败！", Toast.LENGTH_SHORT).show();
                        }

                    }
                    @Override
                    public void onFailure(Call<Collection> call, Throwable t) {

                    }
                });
            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView myfollw_name;

        TextView myfollw_intro;


        RoundImageView imageView_photo;
        LinearLayout myfollow_master_detail;
          Button btn_cancel;
        public ViewHolder(View itemView) {
            super(itemView);

            myfollw_name = (TextView) itemView.findViewById(R.id.tv_my_follow_name);

            myfollw_intro = (TextView) itemView.findViewById(R.id.tv_my_follow_intro);
            imageView_photo=(RoundImageView)itemView.findViewById(R.id.img_my_follow_photo);
            myfollow_master_detail=(LinearLayout)itemView.findViewById(R.id.cardview_my_follow_content);
            btn_cancel=(Button)itemView.findViewById(R.id.my_follow_cancel_btn);
        }
    }
    private void  getUser_id() {
        SharedPreferences sp = mContext.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
      userid= Integer.valueOf(sp.getString("user_id",null));
    }

}
